# Models

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import *
from sqlalchemy.orm import relationship, backref

Base = declarative_base()

class GedDate(Base):
    __tablename__ = 'geddate'

    geddate_id  = Column(Integer, Sequence('geddate_sequence'), primary_key=True)
    year = Column(Integer)    
    month = Column(Integer)    
    day = Column(Integer)    
    hour = Column(Integer)    
    minute = Column(Integer)    
    seconde = Column(Integer) 
    mode = Column(String) 

    def __init__(self):
        self.mode = 'simple'

    def processComplete(self):
        if (self.year > 0 & self.month > 0 & self.day > 0):
            complete_date = datetime(year, month, day)

class Event(Base):
    __tablename__ = 'event'

    event_id  = Column(Integer, Sequence('event_sequence'), primary_key=True)
    type = Column(String)

    date_id = Column(Integer, ForeignKey('geddate.geddate_id'), name="date_id")
    
    geddate = relationship(GedDate, foreign_keys=date_id,
                                      primaryjoin=date_id == GedDate.geddate_id)    
    def __init__(self):
        self.sources = []
        self.notes = []
        self.multimedialinks = []

class FamilyTreeSource(Base):
    __tablename__ = 'familytree_source'
       
    familytree_source_id  = Column(Integer, Sequence('familytree_source_sequence'), primary_key=True)
    system_id = Column(String)
    name = Column(String)
    version = Column(String)
    corporate_id = Column(Integer, ForeignKey('familytree_source_corporate.familytree_source_corporate_id'), name="corporate_id")
    
    def __init__(self):
        pass

class FamilyTree (Base):
    __tablename__ = 'familytree'
    
    familytree_id = Column(Integer, Sequence('familytree_sequence'), primary_key=True)
    charset = Column(String)        
    copyright = Column(String)        
    filename = Column(String, name="filename")        
    langage = Column(String)        
    source_id = Column(Integer, ForeignKey('familytree_source.familytree_source_id'), name="source_id")
    date_id = Column(Integer, ForeignKey('geddate.geddate_id'), name="date_id")
    
    geddate = relationship(GedDate, foreign_keys=date_id,
                                      primaryjoin=date_id == GedDate.geddate_id)    
    source = relationship(FamilyTreeSource, foreign_keys=source_id,
                                      primaryjoin=source_id == FamilyTreeSource.familytree_source_id)    

    def __init__(self):
        self.individuals = []
        self.families = []
        self.sources = []
        self.notes = []
        self.submitters = []

individual_event_table = Table('individual_event', Base.metadata,
    Column('individual_id', Integer, ForeignKey('individual.individual_id')),
    Column('event_id', Integer, ForeignKey('event.event_id'))
)

family_children_table = Table('family_children', Base.metadata,
    Column('family_id', Integer, ForeignKey('family.family_id')),
    Column('individual_id', Integer, ForeignKey('individual.individual_id'))
)

individual_name_individual_table = Table('individual_name_individual', Base.metadata,
    Column('individual_name_id', Integer, ForeignKey('individual_name.individual_name_id')),
    Column('individual_id', Integer, ForeignKey('individual.individual_id'))
)

class Individual_Name (Base):
    __tablename__ = 'individual_name'
    
    individual_name_id = Column(Integer, Sequence('individual_name_sequence'), primary_key=True)
    firstname = Column(String, name="firstname")    
    lastname = Column(String, name="lastname")    
    prefix = Column(String, name="prefix")    
    suffix = Column(String, name="suffix")    
    nickname = Column(String, name="nickname")    
    surname = Column(String, name="surname")    
    surname_prefix = Column(String, name="surname_prefix")    
    
    def __init__(self):
        self.sources = []
        self.notes = []

class Individual (Base):
    __tablename__ = 'individual'
       
    individual_id = Column(Integer, Sequence('individual_sequence'), primary_key=True)
    personal_name = Column(String)    
    sex           = Column(String)
    ident      = Column(String)
    
    events = relationship(Event,  secondary=individual_event_table)
    names = relationship(Individual_Name,  secondary=individual_name_individual_table)
    #familychildren = relationship("Family", secondary=family_children_table)    

    def __init__(self, ident):
        self.ident = ident
        self.events = []
        self.familychildren = []
        self.familyspouse = []
        self.sources = []
        self.objects = []
        self.associations = []

class Family (Base):
    __tablename__ = 'family'
       
    family_id  = Column(Integer, Sequence('family_sequence'), primary_key=True)
    husband_id = Column(Integer, ForeignKey('individual.individual_id'), name="husband_id")
    wife_id    = Column(Integer, ForeignKey('individual.individual_id'), name="wife_id")
    ident      = Column(String)
    
    husband = relationship(Individual, foreign_keys=husband_id,
                                  primaryjoin=husband_id == Individual.individual_id)
    wife = relationship(Individual, foreign_keys=wife_id,
                                  primaryjoin=wife_id == Individual.individual_id)

    children = relationship("Individual",
                        secondary=family_children_table, backref="familychildren")    

    def __init__(self, ident):
        self.ident = ident
        self.children = []
        self.events = []
        self.sources = []

source_event_table = Table('source_event', Base.metadata,
    Column('source_id', Integer, ForeignKey('source.source_id')),
    Column('event_id', Integer, ForeignKey('event.event_id'))
)

class Source (Base):
    __tablename__ = 'source'
    
    source_id    = Column(Integer, Sequence('source_sequence'), primary_key=True)
    ident        = Column(String, name="ident")
    text         = Column(String, name="text")
    title        = Column(String, name="title")
    organisation = Column(String, name="organisation")
    publication  = Column(String, name="publication")

    events = relationship(Event,  secondary=source_event_table)

    def __init__(self, ident):
        self.ident = ident
        self.objects = []

class Note:
    def __init__(self, ident):
        if (ident != None):
            self.ident = ident


class Submitter:
    def __init__(self, ident):
        self.ident = ident

class Place:
    def __init__(self):
        pass

class Map:
    def __init__(self):
        pass

class Address (Base):
    __tablename__ = 'address'
    
    id = Column(Integer, primary_key=True)
    line = Column(String)
    
    def __init__(self):
        pass

class Change:
    def __init__(self):
        self.notes = []


class GedcomInfo:
    def __init__(self):
        pass

class SourceData:
    def __init__(self):
        pass

class Object:
    
    filename = None
    
    def __init__(self):
        pass

    def __init__(self, ident):
        self.ident = ident

class Association:
    def __init__(self):
        self.sources = []
        self.notes = []
        
class Corporation(Base):
    __tablename__ = 'familytree_source_corporate'

    familytree_source_corporate_id  = Column(Integer, Sequence('familytree_source_corporate_sequence'), primary_key=True)
    name = Column(String)
    
    def __init__(self):
        pass

class PhoneNumber:
    def __init__(self):
        pass
    